﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace ServerApplication.UpdatesManager
{
    class UpdatesManager
    {
        public static string FilterPathForName(string pCurrentPath)
        {
            string[] folderName = pCurrentPath.Split('\\');
            string soleFolderName = folderName[folderName.Length - 1];

            return soleFolderName;
        }

        public static void GetUpdates(string pDirectoriesPath, PeerManager.PeerManager pManager)
        {
            while(true)
            {
                string[] allFiles = Directory.GetDirectories(pDirectoriesPath);
                string remainingPath = ConfigurationManager.AppSettings["LastUpdateLocation"];
            
                foreach (string currentPath in allFiles)
                {
                    string fullPath = currentPath + remainingPath;
                    string text = System.IO.File.ReadAllText(fullPath);
                    string peerName = FilterPathForName(currentPath);

                    pManager.mAllStatus[peerName].FromString(text);
                }
                Thread.Sleep(2000);
            }
        }
    }
}
